package Converter;

import VO.Oppgave;
import VO.OppgaveSett;
import DB.DBehandler;
import Hjelpeklasser.VariabelSamarbeid;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

@ManagedBean
@SessionScoped

/**
 * OppgaveConverter; bruker DBehandler
 */
public class OppgaveConverter implements Converter,Serializable{
    private DBehandler db = new DBehandler();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        VariabelSamarbeid varCoop = new VariabelSamarbeid();
        OppgaveSett opgs = varCoop.getOpgs();
        ArrayList<Oppgave> oppgaver;
        if(opgs != null){
            oppgaver = db.hentOppgavefraOsid(opgs);
            for(Oppgave o : oppgaver){
                if(o.getOppgave().equals(value)){
                    return o;
                }
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Oppgave oppg = (Oppgave) value;
        return oppg.toString();
    }
}